The R Spatial Ecosystem

A few main packages are dedicated to spatial data handling like import/export, display, geoprocessing, spatial ststistics.

“Historical” Packages

These packages are still widely used.

Simple Features for R

  • sp, rgeos and rgdal functionnalities in one package.

  • Easier data handling, simpler objects.

  • Main author and maintainer: Edzer Pebesma (also sp author)

  • Compatibility with the pipe synthax and tidyverse operators.

  • First release: October 20, 2016

  • Website: Simple Features for R


sf objects data structure:

format sf

Using sf

Data Import

Reading layer `martinique' from data source `/home/tim/Documents/prz/flo/data/mtq/martinique.shp' using driver `ESRI Shapefile'
Simple feature collection with 34 features and 23 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 690574.4 ymin: 1592426 xmax: 736126.5 ymax: 1645660
epsg (SRID):    32620
proj4string:    +proj=utm +zone=20 +datum=WGS84 +units=m +no_defs

Data Display

Default

Only geometry

Distance Matrix

Units: m
          [,1]     [,2]      [,3]      [,4]      [,5]
[1,]     0.000 35297.56  3091.501 12131.617 17136.310
[2,] 35297.557     0.00 38332.602 25518.913 18605.249
[3,]  3091.501 38332.60     0.000 15094.702 20226.198
[4,] 12131.617 25518.91 15094.702     0.000  7177.011
[5,] 17136.310 18605.25 20226.198  7177.011     0.000

Maps with R

Several solutions are available:

  • ggplot2 users can have a look to ggplot2 mapping features (geom_sf) that can mix nicely with ggspatial.
  • For more advanced mapping features in a ggplot2-like syntax have a look to tmap
  • cartography is based on base graphics and allow most of basic and advanced cartographic representations. Full disclosure: the speaker is the maintainer of cartography.

Here we will focus on cartography and do small examples with ggplot2.

cartography

Data Preparation

Reading layer `OGRGeoJSON' from data source `/home/tim/Documents/prz/flo/data/rhone/seine_maritime.geojson' using driver `GeoJSON'
Simple feature collection with 718 features and 1 field
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 0.06560943 ymin: 49.25139 xmax: 1.790225 ymax: 50.07121
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
Reading layer `OGRGeoJSON' from data source `/home/tim/Documents/prz/flo/data/rhone/dep.geojson' using driver `GeoJSON'
Simple feature collection with 96 features and 2 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -5.138996 ymin: 41.36216 xmax: 9.559598 ymax: 51.089
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs

Exploration of sirene & osm db for restaurants

CRAN Task View: Analysis of Spatial Data

See file data_prep.R for data extraction.

Interactive Visualisation

Explore datasets interactively

The default is not bad:

mapview allow hightly customizable maps:

These maps, as appealing as they seem to be, are not really suitable for displaying geostatistical info. Nonethless they can be really useful for exploratory data analisys.

Count Points in Units

In a regular grid

Create a small function that build a regular grid and count points in each cells.

Create a function that plot a map

Builds grids and display maps

Smooth Density Analysis

Define a function that compute smoothed restaurant density (KDE method).

Define a function that map the results



reproducibility

R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.7.0
LAPACK: /usr/lib/lapack/liblapack.so.3.7.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] mapview_2.5.0       maptools_0.9-3      raster_2.6-7       
 [4] sp_1.3-1            spatstat_1.56-1     rpart_4.1-13       
 [7] nlme_3.1-137        spatstat.data_1.3-1 ggplot2_3.0.0      
[10] cartography_2.1.2   sf_0.6-3            rmdformats_0.3.3   
[13] knitr_1.20         

loaded via a namespace (and not attached):
 [1] viridisLite_0.3.0    shiny_1.1.0          assertthat_0.2.0    
 [4] highr_0.7            stats4_3.5.1         yaml_2.2.0          
 [7] pillar_1.3.0         backports_1.1.2      lattice_0.20-35     
[10] glue_1.3.0           digest_0.6.16        promises_1.0.1      
[13] polyclip_1.9-1       colorspace_1.3-2     htmltools_0.3.6     
[16] httpuv_1.4.5         Matrix_1.2-14        plyr_1.8.4          
[19] pkgconfig_2.0.2      questionr_0.6.3      bookdown_0.7        
[22] purrr_0.2.5          xtable_1.8-2         scales_1.0.0        
[25] webshot_0.5.0        tensor_1.5           satellite_1.0.1     
[28] later_0.7.3          spatstat.utils_1.9-0 tibble_1.4.2        
[31] mgcv_1.8-24          withr_2.1.2          lazyeval_0.2.1      
[34] magrittr_1.5         crayon_1.3.4         mime_0.5            
[37] deldir_0.1-15        evaluate_0.11        foreign_0.8-70      
[40] class_7.3-14         tools_3.5.1          formatR_1.5         
[43] stringr_1.3.1        munsell_0.5.0        bindrcpp_0.2.2      
[46] compiler_3.5.1       e1071_1.7-0          rlang_0.2.2         
[49] classInt_0.2-3       units_0.6-0          grid_3.5.1          
[52] rstudioapi_0.7       htmlwidgets_1.2      goftest_1.1-1       
[55] crosstalk_1.0.0      miniUI_0.1.1.1       base64enc_0.1-3     
[58] labeling_0.3         rmarkdown_1.10       gtable_0.2.0        
[61] abind_1.4-5          DBI_1.0.0            R6_2.2.2            
[64] dplyr_0.7.6          rgeos_0.3-28         bindr_0.1.1         
[67] rprojroot_1.3-2      stringi_1.2.4        Rcpp_0.12.18        
[70] png_0.1-7            leaflet_2.0.1        spData_0.2.9.3      
[73] tidyselect_0.2.4     xfun_0.3